package com.ruoyi.consultation.controller;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consultation.domain.LabConsultationSubject;
import com.ruoyi.consultation.service.ILabConsultationSubjectService;

/**
 * 咨询栏目Controller
 * 
 * @author winner pan
 * @date 2022-03-28
 */
@RestController
@RequestMapping("/consultation/subject")
public class LabConsultationSubjectController extends BaseController {
	@Autowired
	private ILabConsultationSubjectService labConsultationSubjectService;

	/**
	 * 查询咨询栏目列表
	 * 栏目是公共资源，查询不设权限
	 */
	//@PreAuthorize("@ss.hasPermi('consultation:subject:query')")
	@GetMapping("/list")
	public AjaxResult list(LabConsultationSubject labConsultationSubject) {
		// startPage();
		List<LabConsultationSubject> list = labConsultationSubjectService
				.selectLabConsultationSubjectList(labConsultationSubject);
		return AjaxResult.success(list);
	}

	/**
	 * 导出咨询栏目列表
	 */
	//@PreAuthorize("@ss.hasPermi('consultation:subject:export')")
	/*@Log(title = "咨询栏目", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(LabConsultationSubject labConsultationSubject) {
		List<LabConsultationSubject> list = labConsultationSubjectService
				.selectLabConsultationSubjectList(labConsultationSubject);
		ExcelUtil<LabConsultationSubject> util = new ExcelUtil<LabConsultationSubject>(LabConsultationSubject.class);
		return util.exportExcel(list, "subject");
	}*/

	/**
	 * 获取咨询栏目详细信息
	 */
	@PreAuthorize("@ss.hasPermi('consultation:subject:query')")
	@GetMapping(value = "/{subjectId}")
	public AjaxResult getInfo(@PathVariable("subjectId") Long subjectId) {
		return AjaxResult.success(labConsultationSubjectService.selectLabConsultationSubjectById(subjectId));
	}

	/**
	 * 新增咨询栏目
	 */
	@PreAuthorize("@ss.hasPermi('consultation:subject:edit')")
	@Log(title = "咨询栏目", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabConsultationSubject labConsultationSubject) {
		return toAjax(labConsultationSubjectService.insertLabConsultationSubject(labConsultationSubject));
	}

	/**
	 * 修改咨询栏目
	 */
	@PreAuthorize("@ss.hasPermi('consultation:subject:edit')")
	@Log(title = "咨询栏目", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabConsultationSubject labConsultationSubject) {
		return toAjax(labConsultationSubjectService.updateLabConsultationSubject(labConsultationSubject));
	}

	/**
	 * 删除咨询栏目
	 */
	@PreAuthorize("@ss.hasPermi('consultation:subject:edit')")
	@Log(title = "咨询栏目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subjectId}")
	public AjaxResult remove(@PathVariable Long subjectId) {
		return toAjax(labConsultationSubjectService.deleteLabConsultationSubjectById(subjectId));
	}
	@PreAuthorize("@ss.hasPermi('consultation:subject:query')")
	@GetMapping("/list/exclude/{subjectId}")
	public AjaxResult excludeChild(@PathVariable(value = "subjectId", required = false) Long subjectId) {
		List<LabConsultationSubject> subjects = labConsultationSubjectService
				.selectLabConsultationSubjectList(new LabConsultationSubject());
		Iterator<LabConsultationSubject> it = subjects.iterator();
		while (it.hasNext()) {
			LabConsultationSubject d = (LabConsultationSubject) it.next();
			if (d.getSubjectId().intValue() == subjectId
					|| ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), subjectId + "")) {
				it.remove();
			}
		}
		return AjaxResult.success(subjects);
	}
}
